#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QLineEdit>
#include <QKeyEvent>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::setMessage(QString message)
{
    this->ui->textChat->append(message + "\n");
}

QString MainWindow::getMessage()
{
    return this->ui->textEnvoyer->text();
}

void MainWindow::keyPressEvent(QKeyEvent * event)
{
    if(!this->ui->textEnvoyer->text().isEmpty() && (event->key() == Qt::Key_Return))
    {
        r->envoyerDonnees(tr("<strong>") + pseudo + tr("</strong> : ") + getMessage());
        this->ui->textEnvoyer->clear();
    }
}

void MainWindow::setListPseudo(QVector<QString> listPseudo)
{
    switch(listPseudo.size())
    {
        case 1:
                this->ui->pseudo1->setText(listPseudo[0]);
                this->ui->pseudo2->setText("");
                this->ui->pseudo3->setText("");
                this->ui->pseudo4->setText("");
                this->ui->pseudo5->setText("");
                this->ui->pseudo6->setText("");
            break;
        case 2:
                this->ui->pseudo1->setText(listPseudo[0]);
                this->ui->pseudo2->setText(listPseudo[1]);
                this->ui->pseudo3->setText("");
                this->ui->pseudo4->setText("");
                this->ui->pseudo5->setText("");
                this->ui->pseudo6->setText("");
            break;

        case 3:
                this->ui->pseudo1->setText(listPseudo[0]);
                this->ui->pseudo2->setText(listPseudo[1]);
                this->ui->pseudo3->setText(listPseudo[2]);
                this->ui->pseudo4->setText("");
                this->ui->pseudo5->setText("");
                this->ui->pseudo6->setText("");
            break;
        case 4:
                this->ui->pseudo1->setText(listPseudo[0]);
                this->ui->pseudo2->setText(listPseudo[1]);
                this->ui->pseudo3->setText(listPseudo[2]);
                this->ui->pseudo4->setText(listPseudo[3]);
                this->ui->pseudo5->setText("");
                this->ui->pseudo6->setText("");
            break;

        case 5:
                this->ui->pseudo1->setText(listPseudo[0]);
                this->ui->pseudo2->setText(listPseudo[1]);
                this->ui->pseudo3->setText(listPseudo[2]);
                this->ui->pseudo4->setText(listPseudo[3]);
                this->ui->pseudo5->setText(listPseudo[4]);
                this->ui->pseudo6->setText("");
            break;

        case 6:
                this->ui->pseudo1->setText(listPseudo[0]);
                this->ui->pseudo2->setText(listPseudo[1]);
                this->ui->pseudo3->setText(listPseudo[2]);
                this->ui->pseudo4->setText(listPseudo[3]);
                this->ui->pseudo5->setText(listPseudo[4]);
                this->ui->pseudo6->setText(listPseudo[5]);
            break;
    }

}

void MainWindow::setReseau(Reseau *r)
{
    this->r = r;
    m_socket = r->getSocket();
    connect(m_socket, SIGNAL(readyRead()), this, SLOT(donneesRecues()));
    connect(m_socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(erreurSocket(QAbstractSocket::SocketError)));
}

void MainWindow::donneesRecues()
{
    QString message = r->donneesRecues();

    if(message[0] == '&')
    {
        if(!m_listPseudo.contains(message.mid(1,message.size()-2)))
            m_listPseudo.append(message.mid(1,message.size()-2));
        this->setListPseudo(m_listPseudo);
    }else if(message[0] == '#')
    {
        m_listPseudo.remove(m_listPseudo.indexOf(message.mid(1,message.size()-2)));
        this->setListPseudo(m_listPseudo);
    }
    else
        this->setMessage(message);

    if (r->getSocket()->bytesAvailable() > 0)
         donneesRecues();
}


void MainWindow::erreurSocket(QAbstractSocket::SocketError erreur)
{
    this->setMessage(r->erreurSocket(erreur));
}

void MainWindow::setPseudo(QString pseudo)
{
    this->pseudo = pseudo;
}


void MainWindow::on_annuler_clicked()
{

}


void MainWindow::closeEvent(QCloseEvent *event)
 {
     if (QWidget::close ())
     {
         r->envoyerDonnees(tr("#") + this->pseudo + tr("#"));
         r->envoyerDonnees(this->pseudo + r->deconnecte());
     }
 }
